﻿using WRJClient.ViewModel;
using Esri.ArcGISRuntime.Geometry;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WRJClient.Model;
using WRJClient.Common;

namespace WRJClient.UI.Map
{
    public sealed class MapManager
    {
        private BaseMap _map = null;

        private static readonly MapManager _instance = new MapManager();
        public static MapManager Instance { get { return _instance; } }

        public bool IsControlCenterVisable
        {
            get { return ControlCenterManager.Instance.Datas.Any(c => c.Visable); }
            set { foreach (var item in ControlCenterManager.Instance.Datas) { item.Visable = value; } }
        }
        public bool IsWorkStationVisable
        {
            get { return WorkStationManager.Instance.Datas.Any(c => c.Visable); }
            set { foreach (var item in WorkStationManager.Instance.Datas) { item.Visable = value; } }
        }
        public bool IsUAVVisable
        {
            get { return UAVManager.Instance.Datas.Any(c => c.Visable); }
            set { foreach (var item in UAVManager.Instance.Datas) { item.Visable = value; } }
        }
        public bool IsZoneVisable
        {
            get { return ZoneManager.Instance.Datas.Any(c => c.Visable); }
            set { foreach (var item in ZoneManager.Instance.Datas) { item.Visable = value; } }
        }
        public bool IsInfomationVisable
        {
            get { return ZoneManager.Instance.Datas.Any(c => c.Visable); }
            set { foreach (var item in ZoneManager.Instance.Datas) { item.Visable = value; } }
        }

        private MapManager()
        {
        }

        public void Init(BaseMap map)
        {
            if (map != null)
            {
                map.Clear();
                if (_map != null)
                {
                    _map.Clear();
                }
                _map = map;
            }

        }

        public void AddGraphic(string layerName, BaseGraphic graphic)
        {
            _map?.Add(layerName, graphic);
        }

        public void RemoveGraphic(string layerName, BaseGraphic graphic)
        {
            _map?.Remove(layerName, graphic);
        }

        public void SetViewPoint(Geometry geometry)
        {
            try
            {
                _map?.SetViewPoint(geometry);
            }
            catch (Exception)
            {
            }

        }

        public void ChangeMap(int i)
        {
            try
            {
                switch (i)
                {
                    case 0:
                        _map?.ChangeBaseMap(0);
                        break;
                    default:
                        _map?.ChangeBaseMap(1);
                        break;
                }
            }
            catch (Exception)
            {
            }
        }

        public void ZoomIn(int step)
        {
            try
            {
                if (_map != null && !double.IsNaN(_map.mapView.MapScale))
                {
                    _map.mapView.SetViewpointScaleAsync(_map.mapView.MapScale * 1.5);
                }
            }
            catch (Exception)
            {
            }
        }

        public void ZoomOut(int step)
        {
            try
            {
                if (_map != null && !double.IsNaN(_map.mapView.MapScale))
                {
                    _map.mapView.SetViewpointScaleAsync(_map.mapView.MapScale * 0.5);
                }

            }
            catch (Exception)
            {
            }
        }

        public void SetViewPoint(int i)
        {
            try
            {
                switch (i)
                {
                    case 0:
                        SetViewPoint(new Envelope(117.190592626044, 39.3707105737367, 117.279338167999, 39.4161464423523, SpatialReference.Create(4326)));
                        break;
                    case 1:
                        SetViewPoint(new Envelope(117.241254543529, 39.4058329320275, 117.249702295466, 39.4102633867372, SpatialReference.Create(4326)));
                        break;
                    case 2:
                        break;
                    default:
                        SetViewPoint(new Envelope(117.190592626044, 39.3707105737367, 117.279338167999, 39.4161464423523, SpatialReference.Create(4326)));
                        break;
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
